package com.yuexunit.mobilenurse.module.SignInput.presenter.impl;

import com.yuexunit.mobilenurse.base.bean.ActionBean;
import com.yuexunit.mobilenurse.module.SignInput.bean.All_Sqlite_SignInputBean;
import com.yuexunit.mobilenurse.module.SignInput.bean.SignsInput_Data;
import com.yuexunit.mobilenurse.module.SignInput.model.ISignModel;
import com.yuexunit.mobilenurse.module.SignInput.presenter.ISignPresenter;
import com.yuexunit.mobilenurse.module.SignInput.ui.view.IAddSignType;
import com.yuexunit.mobilenurse.module.SignInput.ui.view.ISignInput;
import com.yuexunit.mobilenurse.module.SignInput.xml.SignInputXML;
import com.yuexunit.mobilenurse.util.ProUtil;

import org.kymjs.kjframe.KJDB;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.StringUtils;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by sslcjy on 16/1/26.
 */
public class SignPresenter implements ISignPresenter {

    private IAddSignType addview;
    private ISignInput view;
    private ISignModel model;
    //记录Log
    private final Logger log = ProUtil.getLogger(SignPresenter.class);

    public SignPresenter(ISignModel model, ISignInput view) {
        this.model = model;
        this.view = view;
    }

    public SignPresenter(ISignModel model, IAddSignType addview) {
        this.model = model;
        this.addview = addview;
    }

    @Override
    public void showBaseTypes(String areaId) {
        model.getBaseTypes(areaId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ArrayList<SignsInput_Data>>() {
                    @Override
                    public void onCompleted() {
                        log.info("SignPresenter_showBaseTypes_onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        log.error("SignPresenter_showBaseTypes_onError:" + e);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ArrayList<SignsInput_Data> signsInput_datas) {
                        log.info("SignPresenter_showBaseTypes_onNext:" + signsInput_datas.toString());
//                        view.showSignInputType(signsInput_datas);
                    }
                });
    }

    @Override
    public void showAllTypes(String areaId) {
        model.getAllTypes(areaId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ArrayList<SignsInput_Data>>() {
                    @Override
                    public void onCompleted() {
                        log.info("SignPresenter_showAllTypes_onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        log.error("SignPresenter_showAllTypes_onError:" + e);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ArrayList<SignsInput_Data> signsInput_datas) {
                        log.info("SignPresenter_showAllTypes_onNext:" + signsInput_datas.toString());
                        addview.showAllType(signsInput_datas);
                    }
                });
    }

    @Override
    public void buildUploadSignValue(HashMap signValue,HashMap booldSignValue,ArrayList<SignsInput_Data> all) {
        StringBuilder builder = new StringBuilder();
        String booldValue = "";
        Iterator iter = signValue.entrySet().iterator();
        Iterator boolditer = booldSignValue.entrySet().iterator();
        while (boolditer.hasNext()) {
            Map.Entry entry = (Map.Entry) boolditer.next();
            int key = (int) entry.getKey();
            String val = (String) entry.getValue();
            if (!StringUtils.isEmpty(val)) {
                booldValue += val;
                if (boolditer.hasNext()) {
                    booldValue += "/";
                }
            }
        }
        if (!StringUtils.isEmpty(booldValue)) {
            builder.append("0:").append(booldValue).append(",");
        }
//        while (iter.hasNext()) {
//            Map.Entry entry = (Map.Entry) iter.next();
//            int key = (int) entry.getKey();
//            String val = (String) entry.getValue();
//            if (!StringUtils.isEmpty(val)) {
//                builder.append(all.get(key).getSign_id() + ":" + val);
//                if (iter.hasNext()) {
//                    builder.append(",");
//                }
//            }
//        }
        log.info("SignPresenter_buildUploadSignValue_builder:" + builder.toString());
        view.uploadSignValue(builder.toString());
    }

    @Override
    public void uploadTypesInfo(Map<String, String> praise,String name) {
        if(StringUtils.isEmpty(praise.get("signData"))){
            ViewInject.toast("未填写任何体征上传信息");
            return;
        }
        final All_Sqlite_SignInputBean currentbean = new All_Sqlite_SignInputBean();
        currentbean.setSignData(praise.get("signData"));
        currentbean.setInpNo(praise.get("inpNo"));
        currentbean.setRecorder(praise.get("recorder"));
        currentbean.setTimestamp(praise.get("timestamp"));
        currentbean.setTimepoint(praise.get("timepoint"));
        currentbean.setName(name);
        final KJDB kjdb = KJDB.create();
        kjdb.saveBindId(currentbean);

        model.uploadTypesValue(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ActionBean>() {
                    @Override
                    public void onCompleted() {
                        log.info("SignPresenter_uploadTypesInfo_onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        log.error("SignPresenter_uploadTypesInfo_onError:" + e);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(ActionBean actionBean) {
                        log.info("SignPresenter_uploadTypesInfo_onNext:"+actionBean.toString());
                        if(actionBean.getCode() == 200){
                            kjdb.delete(currentbean);
                            ViewInject.toast("上传体征信息成功.");
                        }else{
                            ViewInject.toast("上传体征信息失败."+actionBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void uploadTypesInfoXML(Map<String, String> praise, String name) {
        final All_Sqlite_SignInputBean currentbean = new All_Sqlite_SignInputBean();
        currentbean.setSignData(praise.get("signData"));
        currentbean.setInpNo(praise.get("inpNo"));
        currentbean.setRecorder(praise.get("recorder"));
        currentbean.setTimestamp(praise.get("timestamp"));
        currentbean.setTimepoint(praise.get("timepoint"));
        currentbean.setName(name);
        final KJDB kjdb = KJDB.create();
        kjdb.saveBindId(currentbean);

        model.uploadTypesValueXML(praise.get("signData"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        log.info("SignPresenter_uploadTypesInfo_onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        log.error("SignPresenter_uploadTypesInfo_onError:"+e);
                        ViewInject.toast("上传体征信息失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(String s) {
                        log.info("SignPresenter_uploadTypesInfo_onNext:"+s);
                        String msg = SignInputXML.getErrorMsg(s);
                        if("".equals(msg)){
                            kjdb.delete(currentbean);
                            ViewInject.toast("上传体征信息成功.");
                        }else{
                            ViewInject.toast("上传体征信息失败:"+msg);
                        }
                    }
                });
    }

}
